import bar from "../bind";
import { Ref } from "vue";
import { Column } from "xcore/data";
import { IPropertyItem } from "xcore/designer/types";

// 创建组件字段
export function createFieldCmpnt(item: Column, checkData: Ref<Record<string, any>>, onUpdate: (field: IPropertyItem, newValue: any) => void): IPropertyItem | null {
  const optCmpnt = bar.designer.find("switch")?.clone();
  if (!optCmpnt) return null; // 如果未找到开关组件，返回null

  // 设置开关组件的属性
  optCmpnt.valueKey = item.name;
  // 创建并返回组件字段对象
  const ncolumns: IPropertyItem = {
    label: item.title,
    data: checkData,
    cmpnt: optCmpnt,
    dbField: {  
      title:item.title,
      name:item.name,
      fieldId:item.column_id||0},
  };

  // 设置开关组件的事件处理函数
  optCmpnt.events = {
    "update:modelValue": (newvalue: boolean) => {
      checkData.value[optCmpnt.valueKey] = newvalue; // 更新开关状态
      onUpdate(ncolumns, newvalue); // 调用更新回调函数
    },
  };
  return ncolumns;
}
